self.addEventListener(
	'message',
	function (e) {
		self.matrixData = e.data.matrixData;
		let basicCanvasPosition = GetOkPosition(e.data.position);
		self.postMessage({
			basicCanvasPosition: basicCanvasPosition
		});
	},
	false
);

function GetOkPosition({ basicCanvasPosition, parCanvasPosition, direction }) {
	//顺时针旋转 ,从左侧开始的数据
	let bool = true;
	let firstBasic = JSON.parse(JSON.stringify(basicCanvasPosition));
	let currentPosition = null;
	if (direction == 'left') {
		currentPosition = 'left';
		basicCanvasPosition.sRow = basicCanvasPosition.sRow - 5;
		basicCanvasPosition.eRow = basicCanvasPosition.eRow - 5;
		bool = isUpperBlack(basicCanvasPosition);
	} else if (direction == 'right') {
		currentPosition = 'right';
		basicCanvasPosition.sRow = basicCanvasPosition.sRow + 5;
		basicCanvasPosition.eRow = basicCanvasPosition.eRow + 5;
		bool = isUpperBlack(basicCanvasPosition);
	}
	while (bool) {
		// 到达初始位置的时候在重置序号基准位置,然后吧basicCanvasPosition整体左移一个位置或者右移自己的位置
		if (isSameObj(firstBasic, basicCanvasPosition)) {
			//重置基准
			parCanvasPosition = whileInResetParCanvasPosition(basicCanvasPosition, parCanvasPosition);
			basicCanvasPosition = whileInResetBasicCanvasPosition(basicCanvasPosition, direction);
			firstBasic = JSON.parse(JSON.stringify(basicCanvasPosition));

			if (direction == 'left') {
				basicCanvasPosition.sRow = basicCanvasPosition.sRow - 5;
				basicCanvasPosition.eRow = basicCanvasPosition.eRow - 5;
				bool = isUpperBlack(basicCanvasPosition);
			} else if (direction == 'right') {
				basicCanvasPosition.sRow = basicCanvasPosition.sRow + 5;
				basicCanvasPosition.eRow = basicCanvasPosition.eRow + 5;
				bool = isUpperBlack(basicCanvasPosition);
			}
			continue;
		}
		//在左边从下往上找
		if (currentPosition == 'left') {
			if (basicCanvasPosition.eRow <= parCanvasPosition.sRow - 1) {
				currentPosition = 'up';
				continue;
			}
			basicCanvasPosition.sRow = basicCanvasPosition.sRow - 5;
			basicCanvasPosition.eRow = basicCanvasPosition.eRow - 5;
			bool = isUpperBlack(basicCanvasPosition);
			//说明从在基准左边从下往上移动已经和基准的开始位置重合了 , 该往右走了
		}
		// 在上边从左往右找
		else if (currentPosition == 'up') {
			if (basicCanvasPosition.sColumn >= parCanvasPosition.eColumn + 1) {
				currentPosition = 'right';
				continue;
			}
			basicCanvasPosition.sColumn = basicCanvasPosition.sColumn + 5;
			basicCanvasPosition.eColumn = basicCanvasPosition.eColumn + 5;
			bool = isUpperBlack(basicCanvasPosition);
		}
		//在右边从上往下找
		else if (currentPosition == 'right') {
			if (basicCanvasPosition.sRow >= parCanvasPosition.eRow + 1) {
				currentPosition = 'down';
				continue;
			}
			basicCanvasPosition.sRow = basicCanvasPosition.sRow + 5;
			basicCanvasPosition.eRow = basicCanvasPosition.eRow + 5;
			bool = isUpperBlack(basicCanvasPosition);
		}
		//在下边从右往左找
		else if (currentPosition == 'down') {
			if (basicCanvasPosition.eColumn <= parCanvasPosition.sColumn - 1) {
				currentPosition = 'left';
				continue;
			}
			basicCanvasPosition.sColumn = basicCanvasPosition.sColumn - 5;
			basicCanvasPosition.eColumn = basicCanvasPosition.eColumn - 5;
			bool = isUpperBlack(basicCanvasPosition);
		}
	}
	return basicCanvasPosition;
}

function isUpperBlack({ sRow, eRow, sColumn, eColumn }) {
	let maxNum = Math.round((eRow - sRow + 1) * (eColumn - sColumn + 1) * 0.1);
	let OneNum = 0;
	let bool = false;
	for (let i = sRow; i <= eRow; i++) {
		if (bool) {
			break;
		}
		for (let j = sColumn; j <= eColumn; j++) {
			if (
				self.matrixData[i] &&
				typeof self.matrixData[i][j] !== 'undefined' &&
				(self.matrixData[i][j] == 2 || self.matrixData[i][j] == 1)
			) {
				bool = true;
				break;
			}
			if (self.matrixData[i] && typeof self.matrixData[i][j] !== 'undefined' && self.matrixData[i][j] == 1) {
				OneNum++;
				if (maxNum < OneNum) {
					bool = true;
					break;
				}
			}
		}
	}
	return bool;
}

function isSameObj(obj1, obj2) {
	return obj1.sRow == obj2.sRow && obj1.eRow == obj2.eRow && obj1.sColumn == obj2.sColumn && obj1.eColumn == obj2.eColumn;
}

//辅助方法--重新设置基准序号在canvas中的位置
function whileInResetParCanvasPosition(basicCanvasPosition, parCanvasPosition) {
	return {
		sRow: parCanvasPosition.sRow - 5,
		// (basicCanvasPosition.eRow - basicCanvasPosition.sRow + 1),
		eRow: parCanvasPosition.eRow + 5,
		// (basicCanvasPosition.eRow - basicCanvasPosition.sRow + 1),
		sColumn: parCanvasPosition.sColumn - 5,
		// (basicCanvasPosition.eColumn - basicCanvasPosition.sColumn + 1),
		eColumn: parCanvasPosition.eColumn + 5
		// (basicCanvasPosition.eColumn - basicCanvasPosition.sColumn + 1),
	};
}
//辅助方法--重新设置文本标签在canvas中的位置
function whileInResetBasicCanvasPosition(basicCanvasPosition, direction) {
	if (direction == 'left') {
		return {
			sRow: basicCanvasPosition.sRow,
			eRow: basicCanvasPosition.eRow,
			sColumn: basicCanvasPosition.sColumn - 5,
			// (basicCanvasPosition.eColumn - basicCanvasPosition.sColumn + 1),
			eColumn: basicCanvasPosition.eColumn - 5
			// (basicCanvasPosition.eColumn - basicCanvasPosition.sColumn + 1),
		};
	} else if (direction == 'right') {
		return {
			sRow: basicCanvasPosition.sRow,
			eRow: basicCanvasPosition.eRow,
			sColumn: basicCanvasPosition.sColumn + 5,
			// (basicCanvasPosition.eColumn - basicCanvasPosition.sColumn + 1),
			eColumn: basicCanvasPosition.eColumn + 5
			// (basicCanvasPosition.eColumn - basicCanvasPosition.sColumn + 1),
		};
	}
}
